package com.zookeeper.basic.one;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by 58 on 2016-7-15.
 * 同步创建zk节点
 */
public class ZkCreateSync implements Watcher {
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public void process(WatchedEvent event) {
        if(Event.KeeperState.SyncConnected == event.getState()){
            System.out.println("keeperstate = "+event.getState());
            System.out.println("keeperpath"+event.getPath());
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZkCreateSync());
        countDownLatch.await();
        String path  = zooKeeper.create("/zk-test-ephemeral-",
                "".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,   //访问控制模式为非安全的，所有人可以连接
                CreateMode.EPHEMERAL);   //创建临时节点
        System.out.println("Success create node : "+path);
        String path2 = zooKeeper.create("/zk-test-ephemeral-",
                "".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);   //创建临时顺序节点
        System.out.println("success create node : "+path2);


    }
}
